﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Threading;
using LibGreen;
using LibGreen.Events;

namespace WindowsFormsApplication1
{
	public partial class Form1 : Form
	{
		public Dictionary<Contact, UserControl> chatSession = new Dictionary<Contact, UserControl> ();
		public Dictionary<IChatRoom, UserControl> chatRooms = new Dictionary<IChatRoom, UserControl> ();
	
		public Form1 ()
		{
			InitializeComponent ();
		
		}
		public LibGreen.Oscar.Session aim;
		public Dispatcher dis = Dispatcher.CurrentDispatcher;
		private void button1_Click (object sender, EventArgs e)
		{
			
			string userName = UserNameBox.Text;
			string password = passwordBox.Text;
			aim = new LibGreen.Oscar.Session (userName, password);
			aim.SessionOpen += OnLogIn;
			aim.SessionFailure += OnFailedLogIn;
			
			//aim.MessageRecieved += OnMessageRecieved;
			aim.LoginStatusUpdate += LoginStatus;
			aim.ContactInfoReceived += BuddyRecieved;
			aim.ChatRoomJoined += aim_ChatRoomJoined;

			aim.Open ();
			setStatusLabel ("loggingIn");
		}

		void aim_ChatRoomJoined (ISessionChatRoom sess, IChatRoom arg)
		{
			System.Diagnostics.Debug.Print ("aim_ChatRoomJoined");
			dis.BeginInvoke (DispatcherPriority.Input,
				new OneArgDelegate (ChatRoomJoined), arg);
		}

		private void ChatRoomJoined (object obj)
		{
			var con = obj as IChatRoom;
			System.Diagnostics.Debug.Print ("ChatRoomJoined");
			this.ChatRoomList.Items.Add (con);
			if (!chatRooms.ContainsKey (con)) {
				var bcp = new ChatRoomPanel (con);
				DialogArea.Controls.Add (bcp);
				chatRooms.Add (con, bcp);
			}
		}

		private void OnLogIn (ISession sess)
		{
			System.Diagnostics.Debug.Print ("OnLogIn");
			dis.BeginInvoke (DispatcherPriority.Input, new OneArgDelegate(setStatusLabel), "Logged IN");
		}
		delegate void OneArgDelegate (object obj);
		private void OnFailedLogIn (ISession sess, SessionFailureArg arg)
		{
			System.Diagnostics.Debug.Print ("OnFailedLogIn");
			dis.BeginInvoke (DispatcherPriority.Input,new OneArgDelegate(setStatusLabel), arg.ErrorCode);
		}

		private void LoginStatus (ISession sess, string message, double percent)
		{
			System.Diagnostics.Debug.Print ("LoginStatus");
			dis.BeginInvoke (DispatcherPriority.Input, new OneArgDelegate (setStatusLabel), message + " " + percent);
		}
		
		private void setStatusLabel (object value)
		{
			System.Diagnostics.Debug.Print ("setStatusLabel");
			StatusLabel.Text = value.ToString();
		}

		private void BuddyRecieved (ISessionContactList sess, Contact con)
		{
			System.Diagnostics.Debug.Print ("BuddyRecieved");
			dis.BeginInvoke (DispatcherPriority.Input,
				new OneArgDelegate (AddContact), con);
		}

		private void AddContact (object obj)
		{
			var con = obj as Contact;
			System.Diagnostics.Debug.Print ("AddContact");
			ContactList.Items.Add (con);
			if (!chatSession.ContainsKey (con)) {
				var bcp = new BuddyChatPanel (con);
				DialogArea.Controls.Add (bcp);
				chatSession.Add (con, bcp);
			}
		}

		private void passwordBox_KeyPress (object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\n' || e.KeyChar == '\r')
				this.button1_Click (this, null);
		}

		private void ContactList_SelectedIndexChanged (object sender, EventArgs e)
		{
			chatSession[ContactList.SelectedItem as Contact].BringToFront();
			
		}

		private void JoinChannelButton_Click (object sender, EventArgs e)
		{
			(aim as ISessionChatRoom).JoinChatRoom (ChannelTextBox.Text);
		}

	}
}
